<?php

/**
 * 公司案例管理
 * @desc
 */
class Api_Case extends PhalApi_Api {

    public function getRules() {
        return [
            'add'       => [
                'token'      => ['name' => 'token', 'type' => 'string', 'min' => 12, 'max' => 12, 'require' => true, 'desc' => 'tokenID'],
                'name'       => ['name' => 'title', 'type' => 'string', 'require' => true, 'desc' => '名称'],
                'organize'   => ['name' => 'organize', 'type' => 'string', 'require' => true, 'desc' => '所属公司'],
                'sort'       => ['name' => 'sort', 'type' => 'string', 'require' => true, 'desc' => '分类'],
                'url'        => ['name' => 'url', 'type' => 'uri', 'require' => true, 'desc' => 'url'],
                'icon'       => ['name' => 'icon', 'type' => 'uri', 'require' => true, 'desc' => 'icon_url'],
                'onlinedate' => ['name' => 'onlinedate', 'type' => 'date', 'require' => true, 'desc' => '上线时间'],
                'isindex'    => ['name' => 'isindex', 'type' => 'boolean', 'default' => false, 'require' => false, 'desc' => '是否首页显示'],
                'ispublic'   => ['name' => 'ispublic', 'type' => 'boolean', 'default' => false, 'require' => false, 'desc' => '是否有公开显示'],
            ],
            'edit'      => [
                'token'      => ['name' => 'token', 'type' => 'string', 'min' => 12, 'max' => 12, 'require' => true, 'desc' => 'tokenID'],
                'id'         => ['name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '案例id'],
                'name'       => ['name' => 'title', 'type' => 'string', 'require' => true, 'desc' => '名称'],
                'organize'   => ['name' => 'organize', 'type' => 'string', 'require' => true, 'desc' => '所属公司'],
                'sort'       => ['name' => 'sort', 'type' => 'string', 'require' => true, 'desc' => '分类'],
                'url'        => ['name' => 'url', 'type' => 'uri', 'require' => true, 'desc' => 'url'],
                'icon'       => ['name' => 'icon', 'type' => 'uri', 'require' => true, 'desc' => 'icon_url'],
                'onlinedate' => ['name' => 'onlinedate', 'type' => 'date', 'require' => true, 'desc' => '上线时间'],
                'isindex'    => ['name' => 'isindex', 'type' => 'boolean', 'default' => false, 'require' => false, 'desc' => '是否首页显示'],
                'ispublic'   => ['name' => 'ispublic', 'type' => 'boolean', 'default' => false, 'require' => false, 'desc' => '是否有公开显示'],
            ],
            'lists'     => [
                'keyword'  => ['name' => "keyword", 'type' => 'string', 'require' => false, 'desc' => '关键词'],
                'sort'     => ['name' => "sort", 'type' => 'string', 'require' => false, 'desc' => '分类'],
                'isindex'  => ['name' => "isindex", 'type' => 'boolean', 'require' => false, 'desc' => '是否首页显示'],
                'ispublic' => ['name' => 'ispublic', 'type' => 'boolean', 'require' => false, 'desc' => '是否有公开显示'],
                'page'     => ['name' => "page", 'type' => 'int', 'default' => 1, 'min' => 1, 'require' => false, 'desc' => '当前第几页'],
                'pageMax'  => ['name' => "pageMax", 'type' => 'int', 'default' => 10, 'min' => 1, 'max' => 100, 'require' => false, 'desc' => '每页多少条'],
                'orderby'  => ['name' => "orderby", 'type' => 'int', 'default' => 4, 'min' => 0, 'max' => 5, 'require' => false, 'desc' => '排序方式 0:"name desc",1:"name asc",2:"organize desc",3:"organize asc",4:"online_date desc",5:"online_date asc"'],
            ],
            'organizes' => [

            ],
            'delete'    => [
                'token' => ['name' => 'token', 'type' => 'string', 'min' => 12, 'max' => 12, 'require' => true, 'desc' => 'tokenID'],
                'id'    => ['name' => 'id', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '案例id'],
            ],
            'info'      => [
                'id' => ['name' => 'id', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '案例id'],
            ]

        ];
    }

    /**
     * 案例添加
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int code
     * @return object data 添加后的数据
     * @return string msg 提示信息
     */
    public function add() {


        $db = new Domain_Case();
        $isadded = $db->isadded($this->url);

        if ($isadded) {
            throw new PhalApi_Exception("该案例已经添加过了", 40001);
        }

        $data = [
            "name"        => $this->name,
            "organize"    => $this->organize,
            "sort"        => $this->sort,
            "url"         => $this->url,
            "icon_url"    => $this->icon,
            "online_date" => $this->onlinedate,
            "isshow"      => $this->isindex ? 1 : 0,
            "ispublic"    => $this->ispublic ? 1 : 0,
        ];


        return $db->add($data);

    }

    /**
     * 更新案例资料
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int code
     * @return boolean data.isUpdated 是否更新成功
     * @return string msg 提示信息
     */
    public function edit() {


        $db = new Domain_Case();
        $hasid = $db->hasid($this->id);

        if (!$hasid) {
            throw new PhalApi_Exception("错误的id", 40002);
        }
        $data = [
            "name"        => $this->name,
            "organize"    => $this->organize,
            "sort"        => $this->sort,
            "url"         => $this->url,
            "icon_url"    => $this->icon,
            "online_date" => $this->onlinedate,
            "isshow"      => $this->isindex ? 1 : 0,
            "ispublic"    => $this->ispublic ? 1 : 0,
        ];

        return [
            "isUpdated" => $db->edit($data, $this->id)
        ];

    }

    /**
     * 案例资料
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int id 案例id
     * @return string name 名称
     * @return string organize 开发商
     * @return string sort 分类
     * @return string url 案例地址
     * @return string iconurl 案例icon
     * @return boolean isindex 是否首页显示
     * @return boolean ispublic 是否首页显示
     * @return date onlinedate 上线时间
     * @return date ctime 添加/更新时间
     * @return string msg 提示信息
     */

    public function info() {
        $db = new Domain_Case();
        return $db->info($this->id);
    }

    /**
     * 案例删除
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return boolean data.isdelete 是否删除成功
     * @return string msg 提示信息
     */
    public function delete() {
        $db = new Domain_Case();
        return [
            "isdelete" => $db->delete($this->id)
        ];
    }


    /**
     * 案例列表
     * @desc 返回数据参见案例资料接口
     * @return int ret HTTP状态码,200成功,其他失败
     * @return array lists 数据列表
     * @return int total 数据总数
     * @return string msg 提示信息
     */
    public function lists() {
        $db = new Domain_Case();

        $rule = [];
        if ($this->keyword) $rule['key'] = $this->keyword;
        if ($this->sort) $rule['sort'] = $this->sort;
        if ($this->isindex!==null) $rule['isindex'] = $this->isindex?true:false;
        if ($this->ispublic!==null) $rule['ispublic'] = $this->ispublic?true:false;
        $page = [
            "page"    => $this->page,
            "pageMax" => $this->pageMax,
            "orderby" => $this->orderby,
        ];
        return $db->getlist($rule, $page);
    }

    /**
     * 获取开发商名字列表
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return array organizes 开发商名称数组
     * @return string msg 提示信息
     */
    public function organizes() {
        $db = new Domain_Case();
        return [
            "organizes" => $db->organizesList()
        ];
    }
}
